package org.weasel.wizards;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.weasel.business.IDFactory;

public class CreateDeveloperWizardPage1 extends WizardPage{
	private Text developerID;
	private Text developerName;
	private Text developerMail;
	private Text developerPosition;
	@SuppressWarnings("unused")
	private ISelection selection;

	public CreateDeveloperWizardPage1(ISelection selection) {
		super("wizardPage");
		setTitle("Developer Wizard");
        setDescription("Add a new developer.");
        this.selection = selection;
        setPageComplete(false);
	}
	
	public void createControl(Composite parent) {
		{
			parent.setSize(525, 223);
		}
		// TODO Auto-generated method stub
		Composite container = new Composite(parent, SWT.NULL);
        GridLayout layout = new GridLayout();
        container.setLayout(layout);
        layout.numColumns = 2;
        layout.verticalSpacing = 9;

        Label label = new Label(container, SWT.NULL);
        label.setText("Developer ID:");

        developerID = new Text(container, SWT.BORDER | SWT.SINGLE);
        developerID.setText(new IDFactory().getUniqueId());
        developerID.setEnabled(false);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        developerID.setLayoutData(gd);
//        developerID.addModifyListener(new ModifyListener() {
//                public void modifyText(ModifyEvent e) {
//                    dialogChanged();
//                }
//            });
        
        
        label = new Label(container, SWT.NULL);
        label.setText("Developer name:");

        developerName = new Text(container, SWT.BORDER | SWT.SINGLE);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        developerName.setLayoutData(gd);
        developerName.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                    dialogChanged();
                }
            });
        
        
        label = new Label(container, SWT.NULL);
        label.setText("Email address:");

        developerMail = new Text(container, SWT.BORDER | SWT.SINGLE);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        developerMail.setLayoutData(gd);
        developerMail.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                    dialogChanged();
                }
            });
        
        
        label = new Label(container, SWT.NULL);
        label.setText("Position:");

        developerPosition = new Text(container, SWT.BORDER | SWT.SINGLE);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        developerPosition.setLayoutData(gd);
        developerPosition.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                    dialogChanged();
                }
            });
        
        
        setControl(container);
    }

    private void dialogChanged() {
    	String errormsg = null;
    	if (getDeveloperPosition().equals(""))
    		errormsg = "Please specify the position of the developer\n";
    	if (getDeveloperMail().equals(""))
    		errormsg = "Please specify the email address of the developer \n";
    	if (getDeveloperName().equals(""))
    		errormsg = "Please specify the name of the developer \n";
    	if (getDeveloperID().equals(""))
    		errormsg = "Please specify the DeveloperID \n";
    	
        updateStatus(errormsg);
    }

    private void updateStatus(String message) {
        setErrorMessage(message);
        setPageComplete(message == null);
    }
    
    @SuppressWarnings("unused")
	private void createLine(Composite parent, int ncol) {
        Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL |
                SWT.BOLD);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan = ncol;
        gridData.verticalIndent = 20;
        line.setLayoutData(gridData);
    }
    
    public String getDeveloperID(){
    	return developerID.getText();
    }
    
    public String getDeveloperName(){
    	return developerName.getText();
    }
    
    public String getDeveloperMail(){
    	return developerMail.getText();
    }
    
    public String getDeveloperPosition(){
    	return developerPosition.getText();
    }
}
